package cate.game.play.util;

import cate.common.table.d.GDCamp;
import cate.game.play.config.AllPlayCfg;
import cate.game.play.config.camp.CampAttrCfg;
import cate.game.play.fighter.HeroFighter;
import cate.game.play.part.FightTeam;

import java.util.HashMap;
import java.util.Map;

public class CampAttrUtil {

	/**
	 * 阵营属性处理
	 * @param config
	 */
	public static void handle(AllPlayCfg config, FightTeam team) {
		Map<Byte, Integer> campNum = new HashMap<>();
		//人数最多的非混沌阵营
		byte maxCamp = 0;
		//最多阵营的人数
		int maxNum = 0;
		//混沌人数
		int chaosNum = 0;

		for (HeroFighter f : team.getAllHero()) {
			Integer curNum = campNum.getOrDefault(f.getCamp(), 0);
			campNum.put(f.getCamp(), ++curNum);
			if (f.getCamp() == GDCamp.Type.C7) {
				chaosNum = curNum;
			}
			if (curNum < maxNum) {
				continue;
			}
			if (curNum == maxNum) {
				if (GDCamp.Type.isNature(maxCamp) && GDCamp.Type.isNiuBi(f.getCamp())) {
					maxCamp = f.getCamp();
					maxNum = curNum;
				}
			} else {
				maxCamp = f.getCamp();
				maxNum = curNum;
			}
		}

		boolean everyCampHasFighter = true; // 每个阵营都有英雄
		if (maxNum > 1 || team.getAllHero().size() < 6) {
			everyCampHasFighter = false;
		}
		if (chaosNum > 0) {
			//混沌代替人数最多的阵营
			if (!everyCampHasFighter) {
				if (maxCamp == GDCamp.Type.NULL) {
					maxCamp = GDCamp.Type.C5;
				}
				campNum.put(maxCamp, maxNum + chaosNum);
			}
			campNum.remove(GDCamp.Type.C7);
		}

		if (everyCampHasFighter) {
			CampAttrCfg cfg = config.campAttr.findFullCamp();
			if (cfg != null) {
				team.addTeamAttr(cfg.attr);
			}
		} else {
			for (Byte camp : campNum.keySet()) {
				CampAttrCfg cfg = config.campAttr.find(camp, campNum.get(camp));
				if (cfg != null) {
					team.addTeamAttr(cfg.attr);
				}
			}
		}
	}
}
